extends Node2D

#种植层
@onready var plant_map: TileMapLayer = %plant
@onready var grass_map: TileMapLayer = %grass
@onready var plant_growth_timer: Timer = $PlantGrowthTimer
var plant_group:Dictionary[Vector2i,int] = {}

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_just_pressed("right_mouse_click"):
		# 获取鼠标置
		var mouse_pos:Vector2 = get_global_mouse_position()
		# 将鼠标位置转化成地图位置
		var map_pos:Vector2i = plant_map.local_to_map(mouse_pos)
		# 获取鼠标置的单个对象(地图中的某个格子)
		var tile_data: TileData = plant_map.get_cell_tile_data(map_pos)
		if tile_data :
			# 获取该单地图对象的属性
			var is_plant: Variant = tile_data.get_custom_data("is_plant")
			if bool(is_plant) == false or exist_plant(map_pos):
				return
			grass_map.set_cell(map_pos,0,Vector2i(0,7))
			# 保存植物坐标
			plant_group[map_pos] = 0


func _on_plant_growth_timer_out() -> void:
	for k in plant_group :
		if plant_group[k] < 4:
			plant_group[k] = plant_group[k] + 1
			grass_map.set_cell(k,0,Vector2i(plant_group[k],7))

# 判断植物是否已存在
func exist_plant(map_pos:Vector2i)->bool:
	if plant_group.size() == 0:
		return false
	else:
		return plant_group.has(map_pos)
